﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Common;
using System.Data;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
    public class WorkCalendarController : AdminBaseController
    {
        /// <summary>
        /// 工作日定义
        /// </summary>
        /// <param name="date">日期</param>
        /// <returns>工作日定义主页视图</returns>
        [AuthorityActionFilter("m_WorkCalendar", Order = 1)]
        [LogPageActionFilter("工作日定义", Order = 2)]
        public ActionResult Index(string date)
        {
            if (date == "" || date == null)
            {
                int year = DateTime.Now.Year;

                string condition = String.Format("FEntityNo='{0}' and YEAR(FWorkDate)={1}", Common.GlobalConstant.ENTITYNO, year);

                BLL.WorkCalendar WorkCalendarBLL = new BLL.WorkCalendar();

                List<Model.WorkCalendar> WorkCalendarList = WorkCalendarBLL.GetModelList(condition);
                if (WorkCalendarList.Count > 0)
                {
                    List<Model.WorkCalendar> SelectWorkCalendarList = (from nm in WorkCalendarList where nm.Fdeftype == "2" select nm).ToList();
                    List<string> YSundayList = new List<string>();
                    foreach (Model.WorkCalendar Model in SelectWorkCalendarList)
                    {
                        YSundayList.Add(Model.FWorkDate.ToString());
                    }
                    OperateSession.addList("YDayList", YSundayList, 120);

                    SelectWorkCalendarList = new List<Model.WorkCalendar>();
                    SelectWorkCalendarList = (from nm in WorkCalendarList where nm.Fdeftype == "3" select nm).ToList();
                    List<string> GSundayList = new List<string>();
                    foreach (Model.WorkCalendar Model in SelectWorkCalendarList)
                    {
                        GSundayList.Add(Model.FWorkDate.ToString());
                    }
                    OperateSession.addList("GDayList", GSundayList, 120);

                    ViewData["Year"] = year;
                }
                else
                {

                    List<string> SundayList = new List<string>();
                    System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");
                    for (int m = 1; m <= 12; m++)
                    {
                        int days = ci.Calendar.GetDaysInMonth(year, m);

                        for (int i = 1; i <= days; i++)
                        {
                            if (new DateTime(year, m, i).DayOfWeek == DayOfWeek.Sunday)
                            {
                                SundayList.Add(DateTime.Parse(year + "-" + m + "-" + i).ToString());
                            }
                            else if (new DateTime(year, m, i).DayOfWeek == DayOfWeek.Saturday)
                            {
                                SundayList.Add(DateTime.Parse(year + "-" + m + "-" + i).ToString());
                            }
                        }
                    }
                    OperateSession.addList("YDayList", SundayList, 120);
                    List<string> GSundayList = new List<string>();
                    OperateSession.addList("GDayList", GSundayList, 120);
                    ViewData["Year"] = year;
                }
            }
            else
            {
                int year = DateTime.Parse(date).Year;
                string date2 = DateTime.Parse(date).ToString();
                List<string> YSundayList = new List<string>();
                List<string> GSundayList = new List<string>();
                YSundayList = OperateSession.getList<string>("YDayList");
                GSundayList = OperateSession.getList<string>("GDayList");
                if (YSundayList.Contains(date2))
                {
                    YSundayList.Remove(date2);

                    GSundayList.Add(date2);
                }
                else if (GSundayList.Contains(date2))
                {
                    GSundayList.Remove(date2);
                }
                else
                {
                    YSundayList.Add(date2);
                }
                OperateSession.addList("YDayList", YSundayList, 120);
                OperateSession.addList("GDayList", GSundayList, 120);
                ViewData["Year"] = year;
            }
            ViewData["jsAlert"] = "";
            return View();
        }

        public ActionResult CalendarPartial(string Calendar12)
        {
            int year = DateTime.Parse(Calendar12).Year;

            string condition = String.Format("FEntityNo='{0}' and YEAR(FWorkDate)={1}", Common.GlobalConstant.ENTITYNO, year);

            BLL.WorkCalendar WorkCalendarBLL = new BLL.WorkCalendar();

            List<Model.WorkCalendar> WorkCalendarList = WorkCalendarBLL.GetModelList(condition);
            if (WorkCalendarList.Count > 0)
            {
                List<Model.WorkCalendar> SelectWorkCalendarList = (from nm in WorkCalendarList where nm.Fdeftype == "2" select nm).ToList();
                List<string> YSundayList = new List<string>();
                foreach (Model.WorkCalendar Model in SelectWorkCalendarList)
                {
                    YSundayList.Add(Model.FWorkDate.ToString());
                }
                OperateSession.addList("YDayList", YSundayList, 120);

                SelectWorkCalendarList = new List<Model.WorkCalendar>();
                SelectWorkCalendarList = (from nm in WorkCalendarList where nm.Fdeftype == "3" select nm).ToList();
                List<string> GSundayList = new List<string>();
                foreach (Model.WorkCalendar Model in SelectWorkCalendarList)
                {
                    GSundayList.Add(Model.FWorkDate.ToString());
                }
                OperateSession.addList("GDayList", GSundayList, 120);

                ViewData["Year"] = year;
            }
            else
            {
                List<string> SundayList = new List<string>();
                System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");
                for (int m = 1; m <= 12; m++)
                {
                    int days = ci.Calendar.GetDaysInMonth(year, m);

                    for (int i = 1; i <= days; i++)
                    {
                        if (new DateTime(year, m, i).DayOfWeek == DayOfWeek.Sunday)
                        {
                            SundayList.Add(DateTime.Parse(year + "-" + m + "-" + i).ToString());
                        }
                        else if (new DateTime(year, m, i).DayOfWeek == DayOfWeek.Saturday)
                        {
                            SundayList.Add(DateTime.Parse(year + "-" + m + "-" + i).ToString());
                        }
                    }
                }
                OperateSession.addList("YDayList", SundayList, 120);
                List<string> GSundayList = new List<string>();
                OperateSession.addList("GDayList", GSundayList, 120);
                ViewData["Year"] = year;
            }
            return PartialView("CalendarPartial");
        }

        public ActionResult Save()
        {
            try
            {
                int year = DateTime.Parse(Fetch.Post("Calendar12")).Year;
                
                List<Model.WorkCalendar> WorkCalendarList = new List<Model.WorkCalendar>();
                System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");
                for (int m = 1; m <= 12; m++)
                {
                    int days = ci.Calendar.GetDaysInMonth(year, m);
                    for (int i = 1; i <= days; i++)
                    {
                        Model.WorkCalendar WorkCalendarModel = new Model.WorkCalendar();
                        WorkCalendarModel.FEntityNo = GlobalConstant.ENTITYNO;

                        List<string> YSundayList = new List<string>();
                        List<string> GSundayList = new List<string>();
                        YSundayList = OperateSession.getList<string>("YDayList");
                        GSundayList = OperateSession.getList<string>("GDayList");
                        WorkCalendarModel.FWorkDate = new DateTime(year, m, i);
                        string date2 = new DateTime(year, m, i).ToString();
                        if (YSundayList.Contains(date2))
                        {
                            WorkCalendarModel.Fdeftype = "2";
                        }
                        else if (GSundayList.Contains(date2))
                        {
                            WorkCalendarModel.Fdeftype = "3";
                        }
                        else
                        {
                            WorkCalendarModel.Fdeftype = "1";
                        }

                        WorkCalendarList.Add(WorkCalendarModel);
                    }
                }
                BLL.WorkCalendar WorkCalendarBLL = new BLL.WorkCalendar();

                WorkCalendarBLL.ADDFWorkDate(GlobalConstant.ENTITYNO, year.ToString(), WorkCalendarList);
                ViewData["Year"] = year;
                ViewData["jsAlert"] = "操作成功";
                return View("Index");
            }
            catch
            {
                int year = int.Parse(Fetch.Post("Year"));
                ViewData["Year"] = year;
                ViewData["jsAlert"] = "操作成功";
                return View("Index");
            }
        }

    }
}
